<?php

namespace App\Admin\Controllers;

use App\Models\Goods;

use App\Models\RbClass;
use App\Models\WebConfig;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

use Encore\Admin\Widgets\Table;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\MessageBag;


class GoodsController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {

        return Admin::content(function (Content $content) {

            $content->header('商品');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        Admin::script($this->script());
        return Admin::grid(Goods::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->title('商品名称');
            $grid->img('图片')->display(function ($data) {
                if (!$data) {
                    return '';
                }
                return <<<DATA
<img src="{$data}" 
style="max-width:50px;max-height:200px" class="img img-thumbnail">
DATA;
            });
            $grid->publisher('出版社');
            $grid->author('作者');
            $grid->price('定价');
            $grid->more('更多')->display(function () {
                $rows = [
                    ['作者简介', $this->author_intro],
                    ['小图', $this->img_small],
                    ['中图', $this->img_medium],
                    ['大图', $this->img_large],
                    ['isbn10', $this->isbn10],
                    ['isbn13', $this->isbn13],
                    ['页数', $this->pages],
                    ['出版日期', $this->pubdate],
                    ['摘要    ', $this->summary],
                    ['created_at', $this->created_at],
                    ['updated_at', $this->updated_at],
                    ['折扣', $this->discount],
                ];

                $table = new Table([], $rows);

                return <<<DATA
<a class="btn btn-xs btn-default grid-expand" data-inserted="0" data-key="{$this->id}" 
data-toggle="collapse" data-target="#grid-collapse-{$this->id}">
    <i class="fa fa-caret-right"></i> 详情
</a>
<template class="grid-expand-{$this->id}">
    <div id="grid-collapse-{$this->id}" class="collapse">
   {$table}
</div>
</template>
DATA;
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Goods::class, function (Form $form) {

            $form->text('title','书名');
            $form->text('author', '作者');
            $form->url('img', '图片');
            $form->text('isbn13', 'isbn13');
            $form->text('price', '价格');
            $form->text('publisher', '出版社');
            $form->text('pubdate', '出版日期');
            $form->text('summary','摘要');
            $form->text('pages', '页数')->default('暂无');

            $form->divide();
            $form->text('author_intro', '作者简介')->default('暂无');
            $form->text('isbn10', 'isbn10')->default('0');
            $form->text('discount','书籍折扣')->default('20');
            $form->saving(function (Form $form) {
                $form->model()->img_small=$form->img;
                $form->model()->img_medium=$form->img;
                $form->model()->img_large=$form->img;
                if($form->isbn13){
                    $haveGoods=Goods::where('isbn13',$form->isbn13)->first();
                    if($haveGoods){
                        $error = new MessageBag([
                            'title'   => '错误提示',
                            'message' => 'isbn已存在',
                        ]);
                        return back()->with(compact('error'));
                    }
                }
                if($form->isbn10){
                    $haveGoods=Goods::where('isbn10',$form->isbn10)->first();
                    if($haveGoods){
                        $error = new MessageBag([
                            'title'   => '错误提示',
                            'message' => 'isbn已存在',
                        ]);
                        return back()->with(compact('error'));
                    }
                }

            });

        });
    }


    /**
     * @return string
     * 更多操作
     */
    protected function script()
    {
        return <<<SCRIPT
$('.grid-expand').on('click', function () {
     if ($(this).data('inserted') == '0') {
        var key = $(this).data('key');
        var row = $(this).closest('tr');
        var html = $('template.grid-expand-'+key).html();

        row.after("<tr><td colspan='"+row.find('td').length+"' style='padding:0 !important; border:0px;'>"+html+"</td></tr>");

        $(this).data('inserted', 1);
    }

    $("i", this).toggleClass("fa-caret-right fa-caret-down");

});

SCRIPT;
    }
}
